package com.haitaoit.chengquan.module.center_service.activity.service_order;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.base.BaseActivity;
import com.haitaoit.chengquan.main.MianButtonActivity;
import com.haitaoit.chengquan.module.center.activity.yuyue.YuyueEvaluateActivity;
import com.haitaoit.chengquan.module.center_service.adapter.service_order.ServiceFuwuAdapter;
import com.haitaoit.chengquan.module.center_service.model.ServiceFuwuModel;
import com.haitaoit.chengquan.module.login.LoginActivity;
import com.haitaoit.chengquan.utils.Constant;
import com.haitaoit.chengquan.utils.DateTimeUtil;
import com.haitaoit.chengquan.utils.DialogUtil;
import com.haitaoit.chengquan.utils.GetSign;
import com.haitaoit.chengquan.utils.HttpUtilsSingle;
import com.haitaoit.chengquan.utils.NameValuePairSign;
import com.haitaoit.chengquan.utils.PreferenceUtils;
import com.haitaoit.chengquan.utils.ToastUtils;
import com.haitaoit.chengquan.view.MyListView;
import com.haitaoit.chengquan.view.PullToRefreshBase;
import com.haitaoit.chengquan.view.PullToRefreshScrollView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ServiceFuwuActivity extends BaseActivity implements PullToRefreshBase.OnRefreshListener<ScrollView> {
    @ViewInject(R.id.fuwu_title_LL1)
    LinearLayout            fuwu_title_LL1;
    @ViewInject(R.id.fuwu_title_LL2)
    LinearLayout            fuwu_title_LL2;
    @ViewInject(R.id.pull_to_refresh_scrollview)
    PullToRefreshScrollView pullrefreshscrollview;
    @ViewInject(R.id.fuwu_all)
    TextView                fuwu_all;
    @ViewInject(R.id.fuwu_nopay)
    TextView                fuwu_nopay;
    @ViewInject(R.id.fuwu_already_pay)
    TextView                fuwu_already_pay;
    @ViewInject(R.id.fuwu_finish)
    TextView                fuwu_finish;
    @ViewInject(R.id.fuwu_all2)
    TextView                fuwu_all2;
    @ViewInject(R.id.fuwu_dqr)
    TextView                fuwu_dqr;
    @ViewInject(R.id.fuwu_yqr)
    TextView                fuwu_yqr;
    @ViewInject(R.id.fuwu_yzf)
    TextView                fuwu_yzf;
    @ViewInject(R.id.fuwu_ywc)
    TextView                fuwu_ywc;
    @ViewInject(R.id.fuwu_line1)
    View                    fuwu_line1;
    @ViewInject(R.id.fuwu_line2)
    View                    fuwu_line2;
    @ViewInject(R.id.fuwu_line3)
    View                    fuwu_line3;
    @ViewInject(R.id.fuwu_line4)
    View                    fuwu_line4;
    @ViewInject(R.id.fuwu_line5)
    View                    fuwu_line5;
    @ViewInject(R.id.all_count)
    TextView                all_count;
    @ViewInject(R.id.not_pay_count)
    TextView                not_pay_count;
    @ViewInject(R.id.have_pay_count)
    TextView                have_pay_count;
    @ViewInject(R.id.completed_count)
    TextView                completed_count;
    @ViewInject(R.id.all_count2)
    TextView                all_count2;
    @ViewInject(R.id.not_pay_count2)
    TextView                not_pay_count2;
    @ViewInject(R.id.have_pay_count2)
    TextView                have_pay_count2;
    @ViewInject(R.id.completed_count2)
    TextView                completed_count2;
    @ViewInject(R.id.confirm_count2)
    TextView                confirm_count2;

    private LinearLayout null_information_layout;
    private MyListView   listView;
    private ScrollView   mScrollView;
    private EditText     search_et;

    private boolean isFrist    = true;
    private int     page       = 1;
    private String  title_type = "1", order_type = "all", order_type_gz = "jc_all";
    private ToastUtils         toast;
    private ServiceFuwuAdapter adapter;
    private Dialog             dlg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_fuwu);
        ViewUtils.inject(this);


        View view = LayoutInflater.from(this).inflate(R.layout.scrollview_listview_layout2, null);
        findViewById(R.id.search_LL).setVisibility(View.VISIBLE);
        null_information_layout = (LinearLayout) view.findViewById(R.id.null_information_layout);
        listView = (MyListView) view.findViewById(R.id.scrollview_listview);
        search_et = (EditText) findViewById(R.id.search_et);
        search_et.setHint("请输入搜索内容...");
        findViewById(R.id.search_img).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (search_et.getText().toString().equals("")) {
                    ToastUtils.showToast("输入搜索内容！");
                } else {
                    isFrist = true;
                    page = 1;
                    if ("1".equals(title_type)) {
                        httpGetmer_service_list(Constant.Getmer_service_list);
                    } else if ("2".equals(title_type)) {
                        httpGetmer_service_list(Constant.Getmer_service_jcgz_list);
                    }
                }
            }
        });

        pullrefreshscrollview.setPullLoadEnabled(true);
        pullrefreshscrollview.setPullRefreshEnabled(true);
        pullrefreshscrollview.setOnRefreshListener(this);
        pullrefreshscrollview.setLastUpdatedLabel(DateTimeUtil
                .getFormatDateTime(DateTimeUtil.getCurrTimestamp()));
        mScrollView = pullrefreshscrollview.getRefreshableView();
        mScrollView.setFillViewport(true);
        mScrollView.setVerticalScrollBarEnabled(false);
        mScrollView.addView(view);

        adapter = new ServiceFuwuAdapter(this);
        adapter.setTitle_type(title_type);
        adapter.setBackCall(backCall);
        listView.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        isFrist = true;
        page = 1;
        if ("1".equals(title_type)) {
            findViewById(R.id.search_LL).setVisibility(View.GONE);
            // 企业服务商的服务订单列表
            httpGetmer_service_list(Constant.Getmer_service_list);
        } else if ("2".equals(title_type)) {
            httpGetmer_service_list(Constant.Getmer_service_jcgz_list);
        }
        super.onResume();
    }

    @OnClick({R.id.left, R.id.fuwu_rb1, R.id.fuwu_rb2, R.id.fuwu_all,
            R.id.fuwu_nopay, R.id.fuwu_already_pay, R.id.fuwu_finish, R.id.fuwu_all2,
            R.id.fuwu_dqr, R.id.fuwu_yqr, R.id.fuwu_yzf, R.id.fuwu_ywc})
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.left:
                //                //                if (getIntent().getIntExtra("from", 0) == 1) {
                //                //                    intent.setClass(ServiceFuwuActivity.this, MainFragmentActivity.class);
                //                //                    intent.putExtra("index", 3);
                //                //                    startActivity(intent);
                //                //                }
                //
                //                if (TextUtils.isEmpty(PreferenceUtils.getPrefString("account_type", "")) || TextUtils.equals("1", PreferenceUtils.getPrefString("account_type", ""))) {
                //
                //                    //                    intent.setClass(this, MainFragmentActivity.class);
                //                    //                    intent.putExtra("index", 0);
                //                    //                    startActivity(intent);
                //                    intent.setClass(this, MainTabActivity.class);
                //                    intent.putExtra("index", 0);
                //                    startActivity(intent);
                //                } else {
                //                    intent.setClass(this, MainTabActivityTwo.class);
                //                    intent.putExtra("index", 0);
                //                    startActivity(intent);
                //                }
                finish();
                break;
            case R.id.fuwu_rb1:// 机床维修
                fuwu_title_LL1.setVisibility(View.VISIBLE);
                fuwu_title_LL2.setVisibility(View.GONE);
                setColor();
                fuwu_line5.setVisibility(View.GONE);
                title_type = "1";
                if (order_type.equals("all")) {
                    fuwu_all.setTextColor(getResources().getColor(R.color.app_color));
                    fuwu_line1.setVisibility(View.VISIBLE);
                } else if (order_type.equals("nopay")) {
                    fuwu_nopay.setTextColor(getResources().getColor(R.color.app_color));
                    fuwu_line2.setVisibility(View.VISIBLE);
                } else if (order_type.equals("already_pay")) {
                    fuwu_already_pay.setTextColor(getResources().getColor(R.color.app_color));
                    fuwu_line3.setVisibility(View.VISIBLE);
                } else if (order_type.equals("finish")) {
                    fuwu_finish.setTextColor(getResources().getColor(R.color.app_color));
                    fuwu_line4.setVisibility(View.VISIBLE);
                }
                adapter.setTitle_type(title_type);
                isFrist = true;
                page = 1;
                httpGetmer_service_list(Constant.Getmer_service_list);
                break;
            case R.id.fuwu_rb2:// 机床改造
                fuwu_title_LL1.setVisibility(View.GONE);
                fuwu_title_LL2.setVisibility(View.VISIBLE);
                setColor();
                title_type = "2";
                if (order_type_gz.equals("jc_all")) {
                    fuwu_all2.setTextColor(getResources().getColor(R.color.app_color));
                    fuwu_line1.setVisibility(View.VISIBLE);
                } else if (order_type_gz.equals("jc_no_qr")) {
                    fuwu_dqr.setTextColor(getResources().getColor(R.color.app_color));
                    fuwu_line2.setVisibility(View.VISIBLE);
                } else if (order_type_gz.equals("jc_qr")) {
                    fuwu_yqr.setTextColor(getResources().getColor(R.color.app_color));
                    fuwu_line3.setVisibility(View.VISIBLE);
                } else if (order_type_gz.equals("jc_pay")) {
                    fuwu_yzf.setTextColor(getResources().getColor(R.color.app_color));
                    fuwu_line4.setVisibility(View.VISIBLE);
                } else if (order_type_gz.equals("jc_finish")) {
                    fuwu_ywc.setTextColor(getResources().getColor(R.color.app_color));
                    fuwu_line5.setVisibility(View.VISIBLE);
                }
                adapter.setTitle_type(title_type);
                isFrist = true;
                page = 1;
                httpGetmer_service_list(Constant.Getmer_service_jcgz_list);
                break;
            case R.id.fuwu_all:///////// 全部 机床维修
                setColor();
                fuwu_all.setTextColor(getResources().getColor(R.color.app_color));
                fuwu_line1.setVisibility(View.VISIBLE);
                fuwu_line5.setVisibility(View.GONE);

                order_type = "all";
                isFrist = true;
                page = 1;
                httpGetmer_service_list(Constant.Getmer_service_list);
                break;
            case R.id.fuwu_nopay:// 未支付
                setColor();
                fuwu_nopay.setTextColor(getResources().getColor(R.color.app_color));
                fuwu_line2.setVisibility(View.VISIBLE);
                fuwu_line5.setVisibility(View.GONE);

                order_type = "nopay";
                isFrist = true;
                page = 1;
                httpGetmer_service_list(Constant.Getmer_service_list);
                break;
            case R.id.fuwu_already_pay:// 已支付
                setColor();
                fuwu_already_pay.setTextColor(getResources().getColor(R.color.app_color));
                fuwu_line3.setVisibility(View.VISIBLE);
                fuwu_line5.setVisibility(View.GONE);

                order_type = "already_pay";
                isFrist = true;
                page = 1;
                httpGetmer_service_list(Constant.Getmer_service_list);
                break;
            case R.id.fuwu_finish:// 已完成
                setColor();
                fuwu_finish.setTextColor(getResources().getColor(R.color.app_color));
                fuwu_line4.setVisibility(View.VISIBLE);
                fuwu_line5.setVisibility(View.GONE);

                order_type = "finish";
                isFrist = true;
                page = 1;
                httpGetmer_service_list(Constant.Getmer_service_list);
                break;
            case R.id.fuwu_all2:///////// 全部 机床改造
                setColor();
                fuwu_all2.setTextColor(getResources().getColor(R.color.app_color));
                fuwu_line1.setVisibility(View.VISIBLE);

                order_type_gz = "jc_all";
                isFrist = true;
                page = 1;
                httpGetmer_service_list(Constant.Getmer_service_jcgz_list);
                break;
            case R.id.fuwu_dqr:// 待确认
                setColor();
                fuwu_dqr.setTextColor(getResources().getColor(R.color.app_color));
                fuwu_line2.setVisibility(View.VISIBLE);

                order_type_gz = "jc_no_qr";
                isFrist = true;
                page = 1;
                httpGetmer_service_list(Constant.Getmer_service_jcgz_list);
                break;
            case R.id.fuwu_yqr:// 已确认
                setColor();
                fuwu_yqr.setTextColor(getResources().getColor(R.color.app_color));
                fuwu_line3.setVisibility(View.VISIBLE);

                order_type_gz = "jc_qr";
                isFrist = true;
                page = 1;
                httpGetmer_service_list(Constant.Getmer_service_jcgz_list);
                break;
            case R.id.fuwu_yzf:// 已支付
                setColor();
                fuwu_yzf.setTextColor(getResources().getColor(R.color.app_color));
                fuwu_line4.setVisibility(View.VISIBLE);

                order_type_gz = "jc_pay";
                isFrist = true;
                page = 1;
                httpGetmer_service_list(Constant.Getmer_service_jcgz_list);
                break;
            case R.id.fuwu_ywc:// 已完成
                setColor();
                fuwu_ywc.setTextColor(getResources().getColor(R.color.app_color));
                fuwu_line5.setVisibility(View.VISIBLE);

                order_type_gz = "jc_finish";
                isFrist = true;
                page = 1;
                httpGetmer_service_list(Constant.Getmer_service_jcgz_list);
                break;
        }
    }

    BackCall backCall = new BackCall() {
        @Override
        public void deal(int which, Object... obj) {
            Intent intent = new Intent();
            final int position = (Integer) obj[0];
            switch (which) {
                case R.id.fuwu_itemll:// 详情
                case R.id.fuwu_check_eva:// 查看评价
                    //                case R.id.scrollview_listview:
                    intent.setClass(ServiceFuwuActivity.this, ServiceFuwuDetailActivity.class);
                    intent.putExtra("title_type", title_type);
                    intent.putExtra("order_no", adapter.getList().get(position).getOrder_no());
                    startActivity(intent);
                    break;
                case R.id.fuwu_to_eva:// 评价需求商
                    intent.setClass(ServiceFuwuActivity.this, YuyueEvaluateActivity.class);
                    intent.putExtra("order_no", adapter.getList().get(position).getOrder_no());
                    intent.putExtra("type", "2");
                    intent.putExtra("myimage_uri", adapter.getList().get(position).getTitile_img());
                    startActivity(intent);
                    break;
                case R.id.confirm_complete:// 服务完成
                    dlg = DialogUtil.showInfoDialog1(ServiceFuwuActivity.this, "提示",
                            "您确定该订单服务已完成吗？", "确定", "还没有", new BackCall() {
                                public void deal(int which, Object... obj) {
                                    switch (which) {
                                        case R.id.confirm:// 确定
                                            httpGetmer_service_complete_order(adapter.getList().get(position).getOrder_no());
                                            break;
                                    }
                                    super.deal(which);
                                }
                            });
                    dlg.setCanceledOnTouchOutside(true);
                    dlg.show();
                    break;
                case R.id.detele_fuwu:// 删除订单
                    dlg = DialogUtil.showInfoDialog1(ServiceFuwuActivity.this, "提示",
                            "您确定要删除该服务订单吗？", "确定", "取消", new BackCall() {
                                public void deal(int which, Object... obj) {
                                    switch (which) {
                                        case R.id.confirm:// 确定
                                            httpGetdel_order(adapter.getList().get(position).getOrder_no());
                                            break;
                                    }
                                    super.deal(which);
                                }
                            });
                    dlg.setCanceledOnTouchOutside(true);
                    dlg.show();
                    break;
                case R.id.fuwu_submit_order:// 确认订单
                    dlg = DialogUtil.showInfoDialog1(ServiceFuwuActivity.this, "提示", "您要确认订单吗？", "确定", "取消", new BackCall() {
                        public void deal(int which, Object... obj) {
                            switch (which) {
                                case R.id.confirm:// 确定
                                    httpGetmer_service_confirm_order(adapter.getList().get(position).getOrder_no());
                                    break;
                            }
                            super.deal(which);
                        }
                    });
                    dlg.setCanceledOnTouchOutside(true);
                    dlg.show();
                    break;
                case R.id.fuwu_update_order:// 修改订单
                    Dialog dlg = DialogUtil.ShowUpdateOrderDialog(ServiceFuwuActivity.this, new BackCall() {
                        @Override
                        public void deal(int which, Object... obj) {
                            String price = (String) obj[0];
                            switch (which) {
                                case R.id.submit_price:
                                    httpGetmer_service_update_order_amount(adapter.getList().get(position).getOrder_no(), price);
                                    break;
                            }
                            super.deal(which, obj);
                        }
                    });
                    dlg.setCanceledOnTouchOutside(true);
                    dlg.show();

                    DisplayMetrics metric = new DisplayMetrics();
                    ServiceFuwuActivity.this.getWindowManager().getDefaultDisplay().getMetrics(metric);
                    int width = metric.widthPixels; // 屏幕宽度（像素）
                    WindowManager.LayoutParams params = dlg.getWindow().getAttributes();
                    params.width = width;
                    params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
                    dlg.getWindow().setAttributes(params);
                    break;
            }
            super.deal(which, obj);
        }
    };

    // 企业服务商列表
    private void httpGetmer_service_list(String url) {
        int status = 0;// 分类0全部1未支付2已支付3已完成4已评价
        if ("1".equals(title_type)) {
            if ("all".equals(order_type)) {
                status = 0;
            } else if ("nopay".equals(order_type)) {
                status = 1;
            } else if ("already_pay".equals(order_type)) {
                status = 2;
            } else if ("finish".equals(order_type)) {
                status = 3;
            }
        } else if ("2".equals(title_type)) {
            if ("jc_all".equals(order_type_gz)) {
                status = 0;
            } else if ("jc_no_qr".equals(order_type_gz)) {
                status = 1;
            } else if ("jc_qr".equals(order_type_gz)) {
                status = 2;
            } else if ("jc_pay".equals(order_type_gz)) {
                status = 3;
            } else if ("jc_finish".equals(order_type_gz)) {
                status = 4;
            }
        }

        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("user_id", PreferenceUtils.getPrefString("user_id", "")));
        list.add(new NameValuePairSign("status", status + ""));
        list.add(new NameValuePairSign("name", search_et.getText().toString()));
        list.add(new NameValuePairSign("page", page + ""));
        list.add(new NameValuePairSign("pagesize", Constant.PageSize));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("user_id", PreferenceUtils.getPrefString("user_id", ""));
        params.addQueryStringParameter("status", status + "");
        params.addQueryStringParameter("name", search_et.getText().toString());
        params.addQueryStringParameter("page", page + "");
        params.addQueryStringParameter("pagesize", Constant.PageSize);
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, false, url, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String result = arg0.result.toString();
                List<ServiceFuwuModel> new_list = new ArrayList<ServiceFuwuModel>();
                try {
                    JSONObject json = new JSONObject(result);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    Gson gson = new Gson();
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            if (Errmsg.equals("未找到用户信息")) {
                                PreferenceUtils.setPrefString("user_id", "");
                                PreferenceUtils.setPrefString("account_type", "");
                                Intent intent = new Intent(ServiceFuwuActivity.this, LoginActivity.class);
                                intent.putExtra("tag", "2");
                                startActivity(intent);
                                finish();
                            }
                            pullrefreshscrollview.onPullDownRefreshComplete();
                            pullrefreshscrollview.onPullUpRefreshComplete();
                            new_list = new ArrayList<ServiceFuwuModel>();
                            break;
                        case 0:// 成功
                            JSONObject js = new JSONObject(Response);
                            if (title_type.equals("1")) {
                                all_count.setText(js.getString("all_count"));
                                not_pay_count.setText(js.getString("not_pay_count"));
                                have_pay_count.setText(js.getString("have_pay_count"));
                                completed_count.setText(js.getString("completed_count"));

                                if (js.getString("all_count").equals("0")) {
                                    all_count.setVisibility(View.GONE);
                                } else {
                                    all_count.setVisibility(View.VISIBLE);
                                }
                                if (not_pay_count.getText().toString().equals("0")) {
                                    not_pay_count.setVisibility(View.GONE);
                                } else {
                                    not_pay_count.setVisibility(View.VISIBLE);
                                }
                                if (have_pay_count.getText().toString().equals("0")) {
                                    have_pay_count.setVisibility(View.GONE);
                                } else {
                                    have_pay_count.setVisibility(View.VISIBLE);
                                }
                                if (completed_count.getText().toString().equals("0")) {
                                    completed_count.setVisibility(View.GONE);
                                } else {
                                    completed_count.setVisibility(View.VISIBLE);
                                }
                            } else if (title_type.equals("2")) {
                                all_count2.setText(js.getString("all_count"));
                                not_pay_count2.setText(js.getString("not_pay_count"));
                                have_pay_count2.setText(js.getString("have_pay_count"));
                                completed_count2.setText(js.getString("completed_count"));
                                confirm_count2.setText(js.getString("confirm_count"));

                                if (all_count2.getText().toString().equals("0")) {
                                    all_count2.setVisibility(View.GONE);
                                } else {
                                    all_count2.setVisibility(View.VISIBLE);
                                }
                                if (not_pay_count2.getText().toString().equals("0")) {
                                    not_pay_count2.setVisibility(View.GONE);
                                } else {
                                    not_pay_count2.setVisibility(View.VISIBLE);
                                }
                                if (have_pay_count2.getText().toString().equals("0")) {
                                    have_pay_count2.setVisibility(View.GONE);
                                } else {
                                    have_pay_count2.setVisibility(View.VISIBLE);
                                }
                                if (completed_count2.getText().toString().equals("0")) {
                                    completed_count2.setVisibility(View.GONE);
                                } else {
                                    completed_count2.setVisibility(View.VISIBLE);
                                }
                                if (confirm_count2.getText().toString().equals("0")) {
                                    confirm_count2.setVisibility(View.GONE);
                                } else {
                                    confirm_count2.setVisibility(View.VISIBLE);
                                }
                            }

                            new_list = gson.fromJson(js.getString("list_dy"),
                                    new TypeToken<List<ServiceFuwuModel>>() {
                                    }.getType());
                            break;
                        case 2:// 暂无内容
                            pullrefreshscrollview.onPullDownRefreshComplete();
                            pullrefreshscrollview.onPullUpRefreshComplete();
                            new_list = new ArrayList<ServiceFuwuModel>();
                            break;
                    }
                    if (isFrist) {
                        if (isNullDate(new_list)) {
                            adapter.setList(new_list);
                            adapter.notifyDataSetChanged();
                        }
                        pullrefreshscrollview.onPullDownRefreshComplete();
                    } else {
                        List<ServiceFuwuModel> allList = adapter.getList();
                        allList.addAll(new_list);
                        adapter.setList(allList);
                        adapter.notifyDataSetChanged();
                        pullrefreshscrollview.onPullUpRefreshComplete();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }

    // 服务-服务商确认订单(服务商)
    private void httpGetmer_service_confirm_order(String order_no) {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("user_id", PreferenceUtils.getPrefString("user_id", "")));
        list.add(new NameValuePairSign("order_no", order_no));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("user_id", PreferenceUtils.getPrefString("user_id", ""));
        params.addQueryStringParameter("order_no", order_no);
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, false, Constant.Getmer_service_confirm_order, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String jsonString = arg0.result.toString();
                JSONObject json;
                try {
                    json = new JSONObject(jsonString);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            return;
                        case 0:// 成功
                            ToastUtils.showToast(Errmsg);
                            isFrist = true;
                            page = 1;
                            if ("1".equals(title_type)) {
                                httpGetmer_service_list(Constant.Getmer_service_list);
                            } else if ("2".equals(title_type)) {
                                httpGetmer_service_list(Constant.Getmer_service_jcgz_list);
                            }
                            return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }

    // 服务-服务商确认订单(服务商)
    private void httpGetmer_service_update_order_amount(String order_no, String money) {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("user_id", PreferenceUtils.getPrefString("user_id", "")));
        list.add(new NameValuePairSign("order_no", order_no));
        list.add(new NameValuePairSign("money", money));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("user_id", PreferenceUtils.getPrefString("user_id", ""));
        params.addQueryStringParameter("order_no", order_no);
        params.addQueryStringParameter("money", money);
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, false, Constant.Getmer_service_update_order_amount, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String jsonString = arg0.result.toString();
                JSONObject json;
                try {
                    json = new JSONObject(jsonString);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            return;
                        case 0:// 成功
                            ToastUtils.showToast(Errmsg);
                            isFrist = true;
                            page = 1;
                            if ("1".equals(title_type)) {
                                httpGetmer_service_list(Constant.Getmer_service_list);
                            } else if ("2".equals(title_type)) {
                                httpGetmer_service_list(Constant.Getmer_service_jcgz_list);
                            }
                            return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }

    // 服务-服务商确认完成服务(服务商)
    private void httpGetmer_service_complete_order(String order_no) {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("user_id", PreferenceUtils.getPrefString("user_id", "")));
        list.add(new NameValuePairSign("order_no", order_no));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("user_id", PreferenceUtils.getPrefString("user_id", ""));
        params.addQueryStringParameter("order_no", order_no);
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, false, Constant.Getmer_service_complete_order, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String jsonString = arg0.result.toString();
                JSONObject json;
                try {
                    json = new JSONObject(jsonString);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            return;
                        case 0:// 成功
                            ToastUtils.showToast(Errmsg);
                            isFrist = true;
                            page = 1;
                            if ("1".equals(title_type)) {
                                httpGetmer_service_list(Constant.Getmer_service_list);
                            } else if ("2".equals(title_type)) {
                                httpGetmer_service_list(Constant.Getmer_service_jcgz_list);
                            }
                            return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }

    // 删除订单
    private void httpGetdel_order(String order_no) {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("user_id", PreferenceUtils.getPrefString("user_id", "")));
        list.add(new NameValuePairSign("order_no", order_no));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("user_id", PreferenceUtils.getPrefString("user_id", ""));
        params.addQueryStringParameter("order_no", order_no);
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, false, Constant.Getdel_order, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String jsonString = arg0.result.toString();
                JSONObject json;
                try {
                    json = new JSONObject(jsonString);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            return;
                        case 0:// 成功
                            ToastUtils.showToast(Errmsg);
                            isFrist = true;
                            page = 1;
                            if ("1".equals(title_type)) {
                                httpGetmer_service_list(Constant.Getmer_service_list);
                            } else if ("2".equals(title_type)) {
                                httpGetmer_service_list(Constant.Getmer_service_jcgz_list);
                            }
                            return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }

    public void setColor() {
        fuwu_all.setTextColor(getResources().getColor(R.color.black_text));
        fuwu_nopay.setTextColor(getResources().getColor(R.color.black_text));
        fuwu_already_pay.setTextColor(getResources().getColor(R.color.black_text));
        fuwu_finish.setTextColor(getResources().getColor(R.color.black_text));

        fuwu_all2.setTextColor(getResources().getColor(R.color.black_text));
        fuwu_dqr.setTextColor(getResources().getColor(R.color.black_text));
        fuwu_yqr.setTextColor(getResources().getColor(R.color.black_text));
        fuwu_yzf.setTextColor(getResources().getColor(R.color.black_text));
        fuwu_ywc.setTextColor(getResources().getColor(R.color.black_text));

        fuwu_line1.setVisibility(View.INVISIBLE);
        fuwu_line2.setVisibility(View.INVISIBLE);
        fuwu_line3.setVisibility(View.INVISIBLE);
        fuwu_line4.setVisibility(View.INVISIBLE);
        fuwu_line5.setVisibility(View.INVISIBLE);
    }

    /**
     * @tab 判断listview是否有值
     */
    private boolean isNullDate(List<ServiceFuwuModel> rewordList) {
        if (rewordList == null || rewordList.size() == 0) {
            null_information_layout.setVisibility(View.VISIBLE);
            listView.setVisibility(View.GONE);
            return false;
        } else {
            null_information_layout.setVisibility(View.GONE);
            listView.setVisibility(View.VISIBLE);
            return true;
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
        pullrefreshscrollview.setLastUpdatedLabel(DateTimeUtil
                .getFormatDateTime(DateTimeUtil.getCurrTimestamp()));
        isFrist = true;
        page = 1;
        if ("1".equals(title_type)) {
            httpGetmer_service_list(Constant.Getmer_service_list);
        } else if ("2".equals(title_type)) {
            httpGetmer_service_list(Constant.Getmer_service_jcgz_list);
        }
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
        pullrefreshscrollview.setLastUpdatedLabel(DateTimeUtil
                .getFormatDateTime(DateTimeUtil.getCurrTimestamp()));
        isFrist = false;
        page++;
        if ("1".equals(title_type)) {
            httpGetmer_service_list(Constant.Getmer_service_list);
        } else if ("2".equals(title_type)) {
            httpGetmer_service_list(Constant.Getmer_service_jcgz_list);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (getIntent().getIntExtra("from", 0) == 1) {

                Intent intent = new Intent();
                intent.setClass(this, MianButtonActivity.class);
                intent.putExtra("index", 0);
                startActivity(intent);
                //                } else {
                //                    intent.setClass(this, MainTabActivityTwo.class);
                //                    intent.putExtra("index", 0);
                //                    startActivity(intent);
                //                }


                //                Intent intent = new Intent(ServiceFuwuActivity.this, MainFragmentActivity.class);
                //                intent.putExtra("index", 3);
                //                startActivity(intent);
            }
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}
